﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btFileOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog tOFD = new OpenFileDialog();
            tOFD.Filter = "fasta文件|*.fasta";
            if (tOFD.ShowDialog() == DialogResult.OK)
            {
                this.txtFileFast.Text = tOFD.FileName;
            }
        }

        private void btFileSave_Click(object sender, EventArgs e)
        {
            if (this.txtFileFast.Text != "")
            {
                SaveFileDialog tSFD = new SaveFileDialog();
                tSFD.Filter = "fasta文件|*.fasta";
                if (tSFD.ShowDialog() == DialogResult.OK)
                {
                    List<string> tStar = new List<string>();
                    List<string> tCon = new List<string>();
                    using (StreamReader tSW = File.OpenText(this.txtFileFast.Text))
                    {
                        string trrLine = tSW.ReadLine();
                        string tConStr = "";
                        while (trrLine != null)
                        {
                            if (trrLine.StartsWith(">"))
                            {
                                if (tConStr != "")
                                {
                                    tCon.Add(tConStr);
                                    tConStr = "";
                                }
                                tStar.Add(trrLine);
                                
                            }
                            else
                            {
                                tConStr = tConStr + trrLine+Environment.NewLine;
                            }
                            trrLine = tSW.ReadLine();
                        }
                        if (tConStr != "")
                        {
                            tCon.Add(tConStr);
                            tConStr = "";
                        }
                        tSW.Close();
                    }
                    if (tStar.Count == tCon.Count)
                    {
                        using (StreamWriter tSW = File.CreateText(tSFD.FileName))
                        {
                            for (int i = 0; i < tStar.Count; i++)
                            {
                                string str = tStar[i];
                                str = getString(str);
                                tSW.WriteLine(">" + str);
                                tSW.WriteLine(tCon[i]);
                            }
                            tSW.Close();
                        }
                    }
                }
                MessageBox.Show("输出成功");
            }
        }
        private string chr = "gb_";
        private string getString(string str)
        {
            chr = this.txtChar.Text;
            string strResult = "";
            int tCon = str.IndexOf(chr);
            if (tCon > 0)
            {
                string allStr = str.Substring(tCon + 3);
                string[] strArr = allStr.Split(' ');
                if (strArr.Length >= 3)
                {
                    strResult = strArr[0] + " " + strArr[1] + " " + strArr[2];
                }
                else
                {
                    strResult = allStr;
                }
            }
            else
            {
                strResult = str;
            }
            return strResult;
        }
    }
}
